//
//  HomeViewController.m
//  XingMei
//
//  Created by 钱金军 on 2019/3/20.
//  Copyright © 2019年 钱金军. All rights reserved.
//

#import "HomeViewController.h"
#import "FilesHeader.h"


@interface HomeViewController ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>


@end

@implementation HomeViewController
{
    UIScrollView *backScro;
    SDCycleScrollView *bannerView;
    UISearchBar *search;
    UIView *firstFunctionView, *secondFunctionView;
    UIButton *firstBtn, *secondBtn;
    UIButton *jiShiBtn, * liFaBtn;
    UIImageView *advertisingImgView;
    UIImageView *jiShiLine, *liFaLine;
    NSInteger chooseModel;
    NSInteger selected;
    UITableView *technicianListTable, *barberShopTable;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    chooseModel = 0;
    selected = 0;
    [self setHeadView];
    [self setScrollWheelSow];
    [self setFirstFunctionButton];
    [self setSecondFunctionButton];
    [self setSwitchTheViewDot];
    [self setChooseTheTechnicianOrBarberShopBtn];
    [self setCharactersOrTheStoreList];
}

- (void)setHeadView{
    backScro = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -Height_StatusBar, KScreenWidth, KScreenHeight)];
    [self.view addSubview:backScro];
    
    UIImageView *backgroundImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KScreenHeight, 145)];
    [backgroundImgView setImage:[UIImage imageNamed:@"home_img_10"]];
    [backScro addSubview:backgroundImgView];
    
    /** 选择地址 */
    UIButton *addressBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, Height_StatusBar, 35, 25)];
    [addressBtn setTitle:@"嘉兴" forState:UIControlStateNormal];
    [addressBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addressBtn.titleLabel.font = [UIFont fontWithName:ZhongDeng size:13];
    [backScro addSubview:addressBtn];
    [addressBtn addTarget:self action:@selector(clickChooseAddress) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *addressRightImg = [[UIImageView alloc] initWithFrame:CGRectMake(addressBtn.right+2, addressBtn.top+10, 7, 5)];
    [addressRightImg setImage:[UIImage imageNamed:@"home_icon_6"]];
    [backScro addSubview:addressRightImg];
    
    /** 搜索 */
    search = [[UISearchBar alloc] initWithFrame:CGRectMake(addressRightImg.right + 8, addressBtn.top, KScreenWidth - addressRightImg.right - 100, 30)];
    search.layer.masksToBounds = YES;
    search.layer.cornerRadius = 15;
    [search setBackgroundImage:[UIImage new]];
    [search setTranslucent:YES];
    search.layer.backgroundColor = [UIColor whiteColor].CGColor;
    search.placeholder = @"搜索商品、店铺、技师";
    [backScro addSubview:search];
    UITextField * searchField = [search valueForKey:@"_searchField"];
    [searchField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];

    /** 消息 */
    UIButton *messageBtn = [[UIButton alloc] initWithFrame:CGRectMake(search.right + 15, search.top, 30, 25)];
    [messageBtn setImage:[UIImage imageNamed:@"home_icon_8"] forState:UIControlStateNormal];
    [backScro addSubview:messageBtn];
    [messageBtn addTarget:self action:@selector(clickCheckMessage) forControlEvents:UIControlEventTouchUpInside];
    
    /** 扫一扫 */
    UIButton *sacnBtn = [[UIButton alloc] initWithFrame:CGRectMake(messageBtn.right + 10, messageBtn.top, 25, 25)];
    [sacnBtn setImage:[UIImage imageNamed:@"home_icon_9"] forState:UIControlStateNormal];
    [backScro addSubview:sacnBtn];
    [sacnBtn addTarget:self action:@selector(clickSacn) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 轮播滚动
- (void)setScrollWheelSow{
    //轮播滚动视图
    bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(15, search.bottom + 15, KScreenWidth-30, 95) imageURLStringsGroup:nil];
    [backScro addSubview:bannerView];
    bannerView.backgroundColor = [UIColor whiteColor];
    bannerView.autoScrollTimeInterval = 3;
    bannerView.currentPageDotColor = [UIColor colorWithRed:244.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:1.0f];
    bannerView.pageDotColor = [UIColor colorWithRed:7.0f/255.0f green:0.0f/255.0f blue:2.0f/255.0f alpha:0.2f];
    bannerView.delegate = self;
    bannerView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
//    bannerView.localizationImageNamesGroup = [NSArray arrayWithObjects:@"home_img_11",@"home_img_12", nil];
    bannerView.placeholderImage = [UIImage imageNamed:@"home_img_11"];
    [backScro addSubview:bannerView];
}

#pragma mark 第一版面功能按钮
- (void)setFirstFunctionButton{
    firstFunctionView = [[UIView alloc] initWithFrame:CGRectMake(0, bannerView.bottom + 17, KScreenWidth, 120)];
    [backScro addSubview:firstFunctionView];
    
    NSArray *imageArr = [NSArray arrayWithObjects:@"home_icon_10",@"home_icon_14",@"home_icon_13",@"home_icon_12",@"home_icon_11",@"home_icon_10",@"home_icon_14",@"home_icon_13",@"home_icon_12",@"home_icon_11", nil];
    NSArray *titleArr = [NSArray arrayWithObjects:@"美发专区",@"新品特惠",@"折扣优惠",@"护发专区",@"家用工具",@"美发专区",@"新品特惠",@"折扣优惠",@"护发专区",@"家用工具", nil];
    
    for (int i = 0; i < 10; i++) {
        NSInteger index = i % 5;
        NSInteger page = i / 5;
        NSInteger Width_Space = (KScreenWidth-65*5-30)/4;
        NSInteger Height_Space = 1;
        NSInteger btnWidth = 65;
        NSInteger btnHeight = 60;
        NSInteger Start_Y = 0;
        NSInteger Start_X = 15;
        
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(index * (btnWidth + Width_Space) + Start_X, page  * (btnHeight + Height_Space)+Start_Y, btnWidth, btnHeight);
        [btn setTitle:[titleArr objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont fontWithName:ZhongDeng size:11];
        
        [btn setImage:[UIImage imageNamed:[imageArr objectAtIndex:i]] forState:0];
        [btn setImagePositionWithType:SSImagePositionTypeTop spacing:3];
        btn.tag = i;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [firstFunctionView addSubview:btn];
    }
}

#pragma mark 第二版面功能按钮
- (void)setSecondFunctionButton{
    secondFunctionView = [[UIView alloc] initWithFrame:CGRectMake(KScreenWidth, firstFunctionView.top, KScreenWidth, firstFunctionView.frame.size.height)];
    secondFunctionView.backgroundColor = [UIColor redColor];
    [backScro addSubview:secondFunctionView];
    
}

#pragma mark 切换视图圆点
- (void)setSwitchTheViewDot{
    firstBtn = [[UIButton alloc] initWithFrame:CGRectMake(KScreenWidth/2-6, firstFunctionView.bottom + 10, 6, 6)];
    [firstBtn setImage:[UIImage imageNamed:@"home_icon_16"] forState:UIControlStateNormal];
    [backScro addSubview:firstBtn];
    [firstBtn addTarget:self action:@selector(clickChooseFirst) forControlEvents:UIControlEventTouchUpInside];
    
    secondBtn = [[UIButton alloc] initWithFrame:CGRectMake(KScreenWidth/2+6, firstBtn.top, 6, 6)];
    [secondBtn setImage:[UIImage imageNamed:@"home_icon_17"] forState:UIControlStateNormal];
    [backScro addSubview:secondBtn];
    [secondBtn addTarget:self action:@selector(clickChooseSecond) forControlEvents:UIControlEventTouchUpInside];
    
    advertisingImgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, firstBtn.bottom + 15, KScreenWidth-30, 80)];
    [advertisingImgView setImage:[UIImage imageNamed:@"home_img_12"]];
    [backScro addSubview:advertisingImgView];
}

#pragma mark 选择技师或理发店
- (void)setChooseTheTechnicianOrBarberShopBtn{
    /** 技师 */
    jiShiBtn = [[UIButton alloc] initWithFrame:CGRectMake(17, advertisingImgView.bottom + 15, 40, 16)];
    [jiShiBtn setTitle:@"技师" forState:UIControlStateNormal];
    [jiShiBtn setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
    jiShiBtn.titleLabel.font = [UIFont fontWithName:JIACU size:17];
    [backScro addSubview:jiShiBtn];
    jiShiLine = [[UIImageView alloc] initWithFrame:CGRectMake(jiShiBtn.frame.origin.x + (jiShiBtn.frame.size.width/2) - 5, jiShiBtn.bottom + 4, 10, 3)];
    jiShiLine.hidden = NO;
    [jiShiLine setImage:[UIImage imageNamed:@"home_icon_18"]];
    [backScro addSubview:jiShiLine];
    [jiShiBtn addTarget:self action:@selector(chooseTechnician) forControlEvents:UIControlEventTouchUpInside];
    
    /** 理发店 */
    liFaBtn = [[UIButton alloc] initWithFrame:CGRectMake(jiShiBtn.right + 48, jiShiBtn.top, 60, 16)];
    [liFaBtn setTitle:@"理发店" forState:UIControlStateNormal];
    [liFaBtn setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
    liFaBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Heavy" size:15];
    [backScro addSubview:liFaBtn];
    liFaLine = [[UIImageView alloc] initWithFrame:CGRectMake(liFaBtn.frame.origin.x + (liFaBtn.frame.size.width/2) - 5, liFaBtn.bottom + 4, 10, 3)];
    liFaLine.hidden = YES;
    [liFaLine setImage:[UIImage imageNamed:@"home_icon_18"]];
    [backScro addSubview:liFaLine];
    [liFaBtn addTarget:self action:@selector(chooseTheBarberShop) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 人物或店铺列表
- (void)setCharactersOrTheStoreList{
    /** 技师 */
    technicianListTable = [[UITableView alloc] initWithFrame:CGRectMake(0, jiShiLine.bottom + 10, KScreenWidth, KScreenHeight - jiShiLine.bottom -10 + Height_TabBar)];
    technicianListTable.delegate = self;
    technicianListTable.dataSource = self;
    technicianListTable.scrollEnabled = NO;
    [backScro addSubview:technicianListTable];
    /** 设置ScrollView的滚动范围 */
    backScro.contentSize = CGSizeMake(KScreenWidth, technicianListTable.bottom + Height_TabBar + 10);
    
    /** 理发店 */
    barberShopTable = [[UITableView alloc] initWithFrame:CGRectMake(KScreenWidth, technicianListTable.top, KScreenWidth, KScreenHeight - jiShiLine.bottom -10 + Height_TabBar)];
    barberShopTable.delegate = self;
    barberShopTable.dataSource = self;
    barberShopTable.scrollEnabled = NO;
    [backScro addSubview:barberShopTable];
}

#pragma mark TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == technicianListTable) {
        return 2;
    }else if (tableView == barberShopTable){
        return 4;
    }
    else return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 105;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (tableView == technicianListTable) {
        /** 技师列表按钮 */
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];

        UIButton *technicianClosestBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth/2-0.5, view.frame.size.height)];
        [technicianClosestBtn setTitle:@"距离最近" forState:UIControlStateNormal];
        [technicianClosestBtn setTitleColor:rgba(255, 116, 2, 1) forState:UIControlStateNormal];
        technicianClosestBtn.titleLabel.font = [UIFont fontWithName:ZhongDeng size:12];
        [view addSubview:technicianClosestBtn];

        UIButton *technicianHighestScoreBtn = [[UIButton alloc] initWithFrame:CGRectMake(KScreenWidth/2+0.5, technicianClosestBtn.top, technicianClosestBtn.frame.size.width, technicianClosestBtn.frame.size.height)];
        [technicianHighestScoreBtn setTitle:@"评分最高" forState:UIControlStateNormal];
        [technicianHighestScoreBtn setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
        technicianHighestScoreBtn.titleLabel.font = [UIFont fontWithName:ZhongDeng size:12];
        [view addSubview:technicianHighestScoreBtn];

        /** 中间分割线 */
        UIView *segmentationLine = [[UIView alloc] initWithFrame:CGRectMake(KScreenWidth/2, 7, 1, 16)];
        segmentationLine.backgroundColor = rgba(209, 209, 209, 1);
        [view addSubview:segmentationLine];
        /** 底部分割线 */
        UIView *footSegmentationLine = [[UIView alloc] initWithFrame:CGRectMake(0, view.bottom-1, KScreenWidth, 1)];
        footSegmentationLine.backgroundColor = rgba(209, 209, 209, 1);
        [view addSubview:footSegmentationLine];
        
        return view;
    }else{
        /** 理发店列表按钮 */
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
        
        UIButton *barberShopClosestBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth/2-0.5, view.frame.size.height)];
        [barberShopClosestBtn setTitle:@"距离最近" forState:UIControlStateNormal];
        [barberShopClosestBtn setTitleColor:rgba(255, 116, 2, 1) forState:UIControlStateNormal];
        barberShopClosestBtn.titleLabel.font = [UIFont fontWithName:ZhongDeng size:12];
        [view addSubview:barberShopClosestBtn];
        
        UIButton *barberShopHighestScoreBtn = [[UIButton alloc] initWithFrame:CGRectMake(KScreenWidth/2+0.5, barberShopClosestBtn.top, barberShopClosestBtn.frame.size.width, barberShopClosestBtn.frame.size.height)];
        [barberShopHighestScoreBtn setTitle:@"评分最高" forState:UIControlStateNormal];
        [barberShopHighestScoreBtn setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
        barberShopHighestScoreBtn.titleLabel.font = [UIFont fontWithName:ZhongDeng size:12];
        [view addSubview:barberShopHighestScoreBtn];
        
        /** 中间分割线 */
        UIView *segmentationLine = [[UIView alloc] initWithFrame:CGRectMake(KScreenWidth/2, 7, 1, 16)];
        segmentationLine.backgroundColor = rgba(209, 209, 209, 1);
        [view addSubview:segmentationLine];
        /** 底部分割线 */
        UIView *footSegmentationLine = [[UIView alloc] initWithFrame:CGRectMake(0, view.bottom-1, KScreenWidth, 1)];
        footSegmentationLine.backgroundColor = rgba(209, 209, 209, 1);
        [view addSubview:footSegmentationLine];
        
        return view;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TechniciansFromTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[TechniciansFromTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    
    
//    [cell.imageView setImage:[UIImage imageNamed:@"home_icon_10"]];
//
//    if (tableView == barberShopTable) {
//        cell.textLabel.text = @"理发店测试文字";
//    }else cell.textLabel.text = @"测试文字";

    
    return cell;
}

#pragma mark 点击选择地址
- (void)clickChooseAddress{
    
}

#pragma mark 点击查看消息
- (void)clickCheckMessage{
    
}

#pragma mark 点击扫一扫
- (void)clickSacn{
    
}

#pragma mark 点击功能按钮
- (void)clickBtn:(UIButton *)btn{
    
}

#pragma mark 点击选择第一圆点
- (void)clickChooseFirst{
    
    if (selected == 1) {
        [firstBtn setImage:[UIImage imageNamed:@"home_icon_16"] forState:UIControlStateNormal];
        [secondBtn setImage:[UIImage imageNamed:@"home_icon_17"] forState:UIControlStateNormal];
        selected = 0;
        /** 切换到第一版面 */
        firstFunctionView.frame = CGRectMake(0, bannerView.bottom + 17, KScreenWidth, 120);
        secondFunctionView.frame = CGRectMake(-KScreenWidth, bannerView.bottom + 17, KScreenWidth, 120);
    }
}

#pragma mark 点击选择第二圆点
- (void)clickChooseSecond{
    if (selected == 0) {
        [secondBtn setImage:[UIImage imageNamed:@"home_icon_16"] forState:UIControlStateNormal];
        [firstBtn setImage:[UIImage imageNamed:@"home_icon_17"] forState:UIControlStateNormal];
        selected = 1;
        /** 切换到第二版面 */
        firstFunctionView.frame = CGRectMake(-KScreenWidth, bannerView.bottom + 17, KScreenWidth, 120);
        secondFunctionView.frame = CGRectMake(0, bannerView.bottom + 17, KScreenWidth, 120);
    }
}

#pragma mark 选择技师
- (void)chooseTechnician{
    if (chooseModel == 1){
        jiShiBtn.titleLabel.font = [UIFont fontWithName:JIACU size:17];
        liFaBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Heavy" size:15];
        jiShiLine.hidden = NO;
        liFaLine.hidden = YES;
        
        chooseModel = 0;
        
        technicianListTable.frame = CGRectMake(0, jiShiLine.bottom + 10, KScreenWidth, KScreenHeight - jiShiLine.bottom -10 + Height_TabBar);
        barberShopTable.frame = CGRectMake(KScreenWidth, jiShiLine.bottom + 10, KScreenWidth, KScreenHeight - jiShiLine.bottom -10 + Height_TabBar);
        
//        [technicianListTable reloadData];
//        backScro.contentSize = CGSizeMake(KScreenWidth, technicianListTable.bottom + Height_TabBar + 10);
    }
}

#pragma mark 选择理发店
- (void)chooseTheBarberShop{
    if (chooseModel == 0) {
        liFaBtn.titleLabel.font = [UIFont fontWithName:JIACU size:17];
        jiShiBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Heavy" size:15];
        jiShiLine.hidden = YES;
        liFaLine.hidden = NO;
        
        chooseModel = 1;
        
        technicianListTable.frame = CGRectMake(-KScreenWidth, jiShiLine.bottom + 10, KScreenWidth, KScreenHeight - jiShiLine.bottom -10 + Height_TabBar);
        barberShopTable.frame = CGRectMake(0, jiShiLine.bottom + 10, KScreenWidth, KScreenHeight - jiShiLine.bottom -10 + Height_TabBar);
        
//        [barberShopTable reloadData];
//        backScro.contentSize = CGSizeMake(KScreenWidth, barberShopTable.bottom + Height_TabBar + 10);
    }
}









@end
